#include <gtk/gtk.h>   
#include <gst/gst.h>   
#include <signal.h>   
#include <string.h>   
#include <gst/interfaces/xoverlay.h>
#include <gdk/gdkx.h>
#include <dirent.h>
#include <unistd.h>

#include "config.h"
#include "sys_log.h"

static char  file[6][1024];
static int file_index=0;
static int file_count=3;

static GtkWidget *player_jpg_main;
static GtkWidget *player_jpg1;
static GtkWidget *player_jpg2;
static GtkWidget *player_jpg3;

void get_image_size(GtkWidget *widget,int*w,int*h)
{
	gtk_widget_get_size_request(widget,w,h);
/*
	GdkPixbuf *pixbuf = gtk_image_get_pixbuf(GTK_IMAGE(widget));
	*w = gdk_pixbuf_get_width(pixbuf);	
	*h = gdk_pixbuf_get_height(pixbuf);  
	g_object_unref(pixbuf); 
	*/
}

void create_jpg_from_file(GtkWidget *widget, gchar *img_file, int w, int h)
{		
	GdkPixbuf *src_pixbuf;		
	GdkPixbuf *dst_pixbuf;	   
	GtkWidget	*image;
	gint width, height;    


	if(!img_file || -1 == access(img_file,F_OK)) { 
		SYS_LOG("file not exist");
		return;
	}
	SYS_LOG ("create_jpg_from_file in w[%d]h[%d]",w,h);

	src_pixbuf = gdk_pixbuf_new_from_file(img_file, NULL);	  
	width = gdk_pixbuf_get_width(src_pixbuf);	
	height = gdk_pixbuf_get_height(src_pixbuf);    

	if (src_pixbuf ==NULL){   
		  SYS_LOG ("pixbuf create failed");
		  return;
	}
	dst_pixbuf=gdk_pixbuf_scale_simple(src_pixbuf,w,h,GDK_INTERP_BILINEAR);
	gtk_image_set_from_pixbuf((GtkImage *)widget,dst_pixbuf);	 
	g_object_unref(src_pixbuf); 
	g_object_unref(dst_pixbuf);
	
}

void update_image( GtkWidget *widget,char* filename) 
{
	if(NULL == widget || NULL == filename){ 
		SYS_LOG("input error");
		return;
	}
	GdkPixbuf *src_pixbuf;		
	GdkPixbuf *dst_pixbuf;	   
	GtkWidget	*image;
	gint width, height;    


	if(-1 == access(filename,F_OK)){ 
		SYS_LOG("file not exist");
		return;
	}
	get_image_size(widget,&width,&height);

	
	src_pixbuf = gdk_pixbuf_new_from_file(filename, NULL);	  
	if (src_pixbuf ==NULL){   
		  SYS_LOG ("pixbuf create failed");
		  return;
	}
	dst_pixbuf=gdk_pixbuf_scale_simple(src_pixbuf,width,height,GDK_INTERP_BILINEAR);

	gtk_image_set_from_pixbuf((GtkImage *)widget,dst_pixbuf);	 
	g_object_unref(src_pixbuf); 
	g_object_unref(dst_pixbuf);
	gtk_widget_show(widget);
}


void update_image_with_default( GtkWidget *widget,char* filename) 
{
	if(NULL == widget || NULL == filename){ 
		SYS_LOG("input error");
		return;
	}
	GdkPixbuf *src_pixbuf;		
	GdkPixbuf *dst_pixbuf;	   
	GtkWidget	*image;

	if(-1 == access(filename,F_OK)){ 
		SYS_LOG("file not exist");
		return;
	}

	src_pixbuf = gdk_pixbuf_new_from_file(filename, NULL);	  
	if (src_pixbuf ==NULL){   
		  SYS_LOG ("pixbuf create failed");
		  return;
	}
	gtk_image_set_from_pixbuf((GtkImage *)widget,src_pixbuf);	 
	g_object_unref(src_pixbuf); 
	gtk_widget_show(widget);
}



void jpg_player_replace_jpg(gchar* filepath,GtkWidget *image, int w, int h)
{
	GdkPixbuf *src_pixbuf,*dst_pixbuf; 


	if(!filepath || -1 == access(filepath,F_OK)){ 
		SYS_LOG("file not exist");
		return;
	}
	src_pixbuf = gdk_pixbuf_new_from_file(filepath,NULL); 
	if (src_pixbuf ==NULL){   
		  SYS_LOG ("pixbuf create failed");
		  return;
	  }
	dst_pixbuf=gdk_pixbuf_scale_simple(src_pixbuf,w,h,GDK_INTERP_BILINEAR);	
	gtk_image_set_from_pixbuf((GtkImage *)image,dst_pixbuf);	 
	g_object_unref(src_pixbuf); 
	g_object_unref(dst_pixbuf); 
    gtk_widget_show(image);	  

}

static gboolean jpg_player_replace_timeout(void *data)
{

	int f_index = (int)data;
	int w,h;

	if(file_index ==  file_count) file_index = 0;
	get_image_size(player_jpg_main,&w,&h);

	jpg_player_replace_jpg(file[file_index],player_jpg_main,w,h);file_index++;
	
	return TRUE;
}

void play_jpg_player()
{
	int w,h;

	if(player_jpg_main) {
		get_image_size(player_jpg_main,&w,&h);
		create_jpg_from_file(player_jpg_main,file[file_index],w,h);file_index++;
		gtk_widget_show(player_jpg_main);
		g_timeout_add(10000,(GSourceFunc)jpg_player_replace_timeout,(gpointer)file_index);
	}
	
	if(player_jpg1) {
		get_image_size(player_jpg1,&w,&h);
		create_jpg_from_file(player_jpg1,file[3],w,h);
    	gtk_widget_show(player_jpg1);
	}

	if(player_jpg2) {
		get_image_size(player_jpg2,&w,&h);
		create_jpg_from_file(player_jpg2,file[4],w,h);
	    gtk_widget_show(player_jpg2);
	}
	
	if(player_jpg3) {
		get_image_size(player_jpg3,&w,&h);
		create_jpg_from_file(player_jpg3,file[5],w,h);
		gtk_widget_show(player_jpg3);
	}

}

int jpg_player_read_filelist()
{
    DIR *dir;
    struct dirent *ptr;
    char base[1000];
	int i = 0;

	memset(base,'\0',sizeof(base));
	getcwd(base, 999);

	strcat(base,AD_PATH);

	sprintf(file[0],"%s/%s",base,"main1.jpg");
	sprintf(file[1],"%s/%s",base,"main2.jpg");
	sprintf(file[2],"%s/%s",base,"main3.jpg");
	sprintf(file[3],"%s/%s",base,"left.jpg");
	sprintf(file[4],"%s/%s",base,"middle.jpg");
	sprintf(file[5],"%s/%s",base,"right.jpg");
	
    return 1;
}

int jpg_player_update_jpg(char* filepath, char* update_id)
{
	int w,h;
	SYS_LOG("jpg_player_update_jpg[%s][%s]",filepath,update_id);
	if(0 == strcmp(update_id,"main1")) {
		remove(file[0]);
		rename(filepath,file[0]);
	}
	if(0 == strcmp(update_id,"main2")) {
		remove(file[1]);
		rename(filepath,file[1]);
	}
	if(0 == strcmp(update_id,"main3")) {
		remove(file[2]);
		rename(filepath,file[2]);
	}
	if(0 == strcmp(update_id,"left")) {
		remove(file[3]);
		rename(filepath,file[3]);
		get_image_size(player_jpg1,&w,&h);
		jpg_player_replace_jpg(file[3],player_jpg1,w,h);
	}
	if(0 == strcmp(update_id,"middle")) {
		remove(file[4]);
		rename(filepath,file[4]);
		get_image_size(player_jpg2,&w,&h);
		jpg_player_replace_jpg(file[4],player_jpg2,w,h);
	}
	if(0 == strcmp(update_id,"right")) {
		remove(file[5]);
		rename(filepath,file[5]);
		get_image_size(player_jpg3,&w,&h);
		jpg_player_replace_jpg(file[5],player_jpg3,w,h);
	}

	return 1;
}

void play_jpg_player_set_image(GtkWidget *jpg_main,GtkWidget *ad_left,GtkWidget *ad_middle,GtkWidget *ad_right)
{
	player_jpg_main = jpg_main;
	player_jpg1 = ad_left;
	player_jpg2 = ad_middle;
	player_jpg3 = ad_right;
}

